{% extends 'base.html' %}

{% block title %}模型库分类管理{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>模型库分类管理</h2>
        <a href="{{ url_for('model_category.add_category') }}" class="btn btn-primary">添加分类</a>
    </div>

    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
                <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row mb-3">
                <div class="col-md-10">
                    <input type="text" class="form-control me-2" name="search" placeholder="搜索分类名称..." value="{{ search_query }}">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-secondary w-100">搜索</button>
                </div>
            </form>

            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>分类名称</th>
                            <th>层级</th>
                            <th>子分类数量</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for category in categories %}
                        <tr>
                            <td>{{ category.id }}</td>
                            <td>
                                {{ '　' * (category.level - 1) }}|-- {{ category.name }}
                            </td>
                            <td>{{ category.level }}</td>
                            <td>{{ category.child_count }}</td>
                            <td>
                                <div class="btn-group" role="group">
                                    <a href="{{ url_for('model_category.edit_category', id=category.id) }}" class="btn btn-sm btn-outline-primary">编辑</a>
                                    <button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ category.id }}">删除</button>
                                </div>

                                <!-- 删除确认模态框 -->
                                <div class="modal fade" id="deleteModal{{ category.id }}" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                            </div>
                                            <div class="modal-body">
                                                确定要删除分类 <strong>{{ category.name }}</strong> 吗？
                                                {% if category.child_count > 0 %}
                                                <div class="alert alert-warning mt-2">
                                                    注意：此分类包含 {{ category.child_count }} 个子分类，删除后将无法恢复。
                                                </div>
                                                {% endif %}
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                                <form method="post" action="{{ url_for('model_category.delete_category', id=category.id) }}">
                                                    <button type="submit" class="btn btn-danger">删除</button>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        {% else %}
                        <tr>
                            <td colspan="5" class="text-center">暂无分类数据</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}